import os
import time
from django.db import models

# Create your models here.
class Files_Records(models.Model):
    name = models.CharField(max_length=255, default="")
    directory = models.CharField(max_length=4096, default="")
    size = models.IntegerField(default=0)
    update_time = models.IntegerField(default=0)
    create_time = models.IntegerField(default=0)
    type = models.CharField(max_length=20,default="-")
    comments = models.TextField(default="")

    class Meta:
        indexes = [models.Index(fields=['directory','name'])]
        constraints = [models.UniqueConstraint(fields=['directory','name'], name='path')]
        app_label = 'fs'
        managed = False

    @property
    def path(self):
        return os.path.join(self.directory, self.name)
    
    @property
    def modified_time(self):
        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.update_time))

    @property
    def is_dir(self):
        return self.type == "directory"


    def __str__(self) -> str:
        return self.name